package trustedPlatformBackup;

import java.io.BufferedReader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.List;

public class CCMDHandler {

	ProgramProperties properties = new ProgramProperties();

	public ArrayList<String> getCMDOutput(String strCommand) {

		Runtime r = Runtime.getRuntime();
		Process p = null;
		BufferedReader br = null;
		// FileWriter fw = null;
		// BufferedWriter bw = null;
		System.out.println("command: " + strCommand);
		try {
			p = r.exec(strCommand);
			p.getOutputStream().close();
			br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			System.out.println(br.toString());
			String line = null;
			// fw = new FileWriter("c:/output.txt");

			// bw = new BufferedWriter(fw);

			ArrayList<String> alListe = new ArrayList();
			// String Liste f�r den CMD Output
			while ((line = br.readLine()) != null) {
				System.out.println(line);
				// bw.write(line+"\r\n");
				alListe.add(line);
				// Befullen der String Liste mit cmd Output!
			}
			// bw.flush();
			// fw.flush();
			// bw.close();
			// fw.close();
			br.close();
			return alListe;
		} catch (IOException e1) {

			e1.printStackTrace();
			return null;

		}

	}

	public ArrayList<String> getCMDOutput(String[] strCommand) {

		Runtime r = Runtime.getRuntime();
		Process p = null;
		BufferedReader br = null;
		// FileWriter fw = null;
		// BufferedWriter bw = null;
		System.out.println("command[]: " + strCommand);
		try {
			p = r.exec(strCommand);
			p.getOutputStream().close();
			br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			System.out.println(br.toString());
			String line = null;
			// fw = new FileWriter("c:/output.txt");

			// bw = new BufferedWriter(fw);

			ArrayList<String> alListe = new ArrayList();
			// String Liste f�r den CMD Output
			while ((line = br.readLine()) != null) {
				System.out.println(line);
				// bw.write(line+"\r\n");
				alListe.add(line);
				// befüllen der String Liste mit cmd Output!
			}
			// bw.flush();
			// fw.flush();
			// bw.close();
			// fw.close();
			br.close();
			return alListe;
		} catch (IOException e1) {

			e1.printStackTrace();
			return null;

		}

	}

	public ArrayList<File> fillFileList(String strFolder) {
		/*
		 * ArrayList<String> fileList =
		 * getCMDOutput("ls "+strFolder+" -agGh1Q"); Iterator<String> itr =
		 * fileList.iterator(); while (itr.hasNext()) { String element =
		 * itr.next(); //wFileList.add(element); }
		 */
		ArrayList<File> arrFileList = new ArrayList<File>();

		File myDir = new File(strFolder);

		File[] contents = myDir.listFiles();
		if (contents != null) {
			for (File file : contents) {

				arrFileList.add(file);
			}
		}

		return arrFileList;

	}

	public ArrayList<String> findFileVersions(String strFolder, String strSubfolder, String findFolder) {
		String strOptions = "-l";
		String strCMD = "ls " + strOptions + " " + strFolder + File.separator + "*" + strSubfolder + " | grep \"^d\" | grep \"" + findFolder + "\"";

		ArrayList<String> fileList = getCMDOutput(new String[] { strCMD });

		return fileList;

	}

	public ArrayList<String> doFullBackup(String strFolder, String strSubfolder, String findFolder) {
		String strOptions = "-l";
		String strCMD = "ls " + strOptions + " " + strFolder + File.separator + "*" + strSubfolder + " | grep \"^d\" | grep \"" + findFolder + "\"";
		properties.getDoBackupScript();

		ArrayList<String> fileList = getCMDOutput(new String[] { strCMD });

		return fileList;

	}

	void test() {
		String strOptions = "-l";
		String strCMD = "ls " + strOptions + " /home/*/Desktop | grep \"^d\" | grep \"t*\"";
		strCMD = "bash \"ls -l /home/thomas | grep d\"";
		ArrayList<String> fileList = getCMDOutput(strCMD);

	}

	public void makeFullBackup(String prg, boolean isHardlink) {
		String strCMD;
		// TODO Auto-generated method stub
		if (isHardlink) {
			 strCMD ="bash "+ prg + " -H ";
		} else {
			 strCMD = "bash "+prg + "-F ";
		}
		ArrayList<String> fileList = getCMDOutput(strCMD);
	}
}
